﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;  //引用命名空间
using System.IO;   //引用命名空间

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if(Session["flag"]==null)
            {
                Response.Redirect("../../Login.aspx");
            }
           
        } 
    }
    protected void btnBackup_Click(object sender, EventArgs e)
    {
        //默认路径
        string path = @"C:\backup\";
        if (this.TextBox1.Text.Trim().Substring(1, 1) == ":")
        {
            path = this.TextBox1.Text.Trim();
        }
        else
        {
            path = path + this.TextBox1.Text.Trim();
        }
        //关键代码，使用该SQL语句执行数据库备份命令
        string cmdtxt = "backup database db_Material to disk='" + path + ".bak'";
        try
        {
            if(File.Exists(path+".bak"))//判断用户输入的文件是否存在
            {
                Response.Write("<script language=javascript>alert('此文件已存在，请从新输入！')</script>");
                return;
            }
            DB.ExSql(cmdtxt);
            if (this.TextBox1.Text.Trim().Substring(1, 1) == ":")
            {
                Response.Write("<script language=javascript>alert('备份数据成功！')</script>");
            }
            else
            {
                Response.Write("<script language=javascript>alert('备份数据成功,已保存在默认文件夹！')</script>");
                this.Label2.Text = "默认文件夹：　　" + @"\C:\backup\";
            }
            this.TextBox1.Text = "";
           
        }
        catch (Exception ms)
        {
            Response.Write(ms.Message);//捕获异常
            Response.Write("<script language=javascript>alert('备份数据失败！')</script>");

        }
    }
 
}
